import React from 'react';
import { Form, Input, Button } from 'antd';
import Global from '@/components/Global';
import { connect } from 'dva';
import { userId } from '@/constant';
import HobbyWidget from './hobbyWidget';
import moment from 'moment';
const creator = (type, payload) => ({ type, payload });
class Hobby extends React.Component {
  componentDidMount() {
    this.query();
    
  }

  query=()=>{
    // const { dispatch } = this.props;
    // const id = window.sessionStorage.getItem(userId);
    // dispatch(creator('hobby/query', id));
    const {dispatch}=this.props;
    const id = window.sessionStorage.getItem(userId);
    let yStart = moment('0000-01-01');
    let yEnd = moment('9999-12-31');
    let wStart =moment().startOf('week');
    let wEnd = moment().endOf('week');

    dispatch(creator('hobby/getHobbyAndValue', {id,yStart,yEnd,wStart,wEnd}));
  }
  //增加
  add = () => {
    const { dispatch, form } = this.props;
    form.validateFields((err, fields) => {
      if (!err) {
        const id = window.sessionStorage.getItem(userId);
        fields.desc=fields.desc?fields.desc:'无'
        dispatch(creator('hobby/add', { id, ...fields })).then(res=>{
          if(res.httpStatus===200){
            this.props.form.resetFields()
            this.query()
          }
        })
      }
    });
  };
  F;
  render() {
    const { getFieldDecorator } = this.props.form;
    const { handleHobbies } = this.props.hobby;
    return (
      <Global>
        <div>
          <Form layout="inline">
            <Form.Item label="爱好">
              {getFieldDecorator('hobby', {
                rules: [
                  {
                    required: true,
                    message: '请输入待办事项',
                  },
                ],
              })(
                <Input
                  // style={{ width: 750, marginRight: 5 }}
                  // placeholder="请输入待办事项"
                  onKeyDown={this.onKeyDown}
                />,
              )}
            </Form.Item>
            <Form.Item label="描述">
              {getFieldDecorator('desc', {
              })(
                <Input
                  // style={{ width: 750, marginRight: 5 }}
                  // placeholder="请输入待办事项"
                  onKeyDown={this.onKeyDown}
                />,
              )}
            </Form.Item>
            <Form.Item>
              <Button type="primary" onClick={this.add.bind(this)}>
                增加
              </Button>
            </Form.Item>
          </Form>
          <HobbyWidget data={handleHobbies} query={this.query}/>
        </div>
      </Global>
    );
  }
}

const WrappedHobby = Form.create()(Hobby);

export default connect(({ hobby,plan, loading }) => ({ hobby,plan, loading: loading.models.plan }))(
  WrappedHobby,
);
